import { SubjectComponent } from "../../../../core/subject";
import { BulletSide } from "../../physics_math";
import Character from "./character";

const { ccclass, property } = cc._decorator;

export const CHAR_MOVEMENT_STATE = {
    IDLE: 1,
    WALK: 2,
    ATTACK: 3,
};
@ccclass
export default class CharacterCtrl extends SubjectComponent {
    side = 0;
    char: Character = null;
    _ctrlEnabled = true;
    isLeaveTemp = false; //暂离
    get ctrlEnabled() {
        return this._ctrlEnabled;
    }
    set ctrlEnabled(val) {
        if (this._ctrlEnabled === val) {
            return;
        }
        this._ctrlEnabled = val;
        if (this._ctrlEnabled) {
            this.onCtrlEnable();
        }
        else {
            this.onCtrlDisable();
        }
    }
    reset() {

    }
    attach(char: Character) {
        this.char = char;
        this.char.ctrl = this;

        this.ctrlEnabled = true;
        this.char.on("blowup", this.detach, this);
        this.onAttach(char);
    }
    detach() {
        let char = this.char;
        this.char.off(this);
        this.char.ctrl = null;
        this.char = null;
        this.onDetach(char);
    }
    jump() {
        if (this.char.float > 0) {
            this.char.jump(600);
        }
        else {
            this.char.jump(1800);
        }
    }
    use() {
        this.char.use();
    }
    unuse() {
        this.char.unuse();
    }
    grabDrop() {
        if (cc.isValid(this.char.grabedBullet)) {
            this.char.drop();
        }
        else {
            this.char.grab();
        }
    }
    protected lateUpdate(dt: number): void {
        if (!cc.isValid(this.char)) {
            return;
        }
        if (!this.ctrlEnabled) {
            return;
        }

        let power = 22;
        let hSide = BulletSide.None;
        let vSide = BulletSide.None;
        if (this.side & 0b10) {
            hSide = BulletSide.Right;
        }
        else if (this.side & 0b1000) {
            hSide = BulletSide.Left;
        }
        if (this.side & 0b100) {
            vSide = BulletSide.Bottom;
        }
        if (this.side & 0b1) {
            vSide = BulletSide.Top;
        }

        this.char.hLookAt = hSide;
        this.char.vLookAt = vSide;
        let v2 = cc.v2();
        switch (vSide) {
            case BulletSide.Top:
                if (this.char.float > 0) {
                    if (this.char.speed.y < 0) {
                        power *= 3;
                    }
                    v2.y += power;
                }
                break;
            case BulletSide.Bottom:
                if (this.char.float > 0) {
                    if (this.char.speed.y > 0) {
                        power *= 3;
                    }
                    v2.y -= power;
                }
                break;
        }
        switch (hSide) {
            case BulletSide.Right:
                if (this.char.speed.x < 0) {
                    power *= 3;
                }
                v2.x += power;
                break;
            case BulletSide.Left:
                if (this.char.speed.x > 0) {
                    power *= 3;
                }
                v2.x -= power;
                break;
        }
        if (v2.equals(cc.Vec2.ZERO)) {
            this.char.idle();
            this.char.slippy = 0;
        }
        else {
            this.char.move(v2);
            this.char.slippy = 20;
        }

    }

    addUI(uiNode: cc.Node) {
        this.node.addChild(uiNode);
    }

    onAttach(char: Character) { }
    onDetach(char: Character) { }
    protected onCtrlEnable() { }
    protected onCtrlDisable() { }
}